package org.common.controller.base;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.common.exception.DtsException;
import org.common.model.vo.TokenInfoVO;
import org.common.utils.JwtUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;
import java.util.function.Supplier;

/**
 * @Description
 * @Date 2022/12/22 10:47
 * @Author pq
 */
@Slf4j
public class BaseController extends CommonOprController {

    public OR<Void> successResult() {
        OR<Void> or = new OR<>();
        or.setResult(true)
                .setHttpStatus(HttpStatus.OK.value())
                .setCode(String.valueOf(HttpStatus.OK.value()))
                .setMsg("成功");
        return or;
    }

    public ResponseEntity<OR<Void>>  success() {
        OR<Void> or = new OR<>();
        or.setResult(true)
                .setHttpStatus(HttpStatus.OK.value())
                .setCode(String.valueOf(HttpStatus.OK.value()))
                .setMsg(StringUtils.EMPTY);
        return new ResponseEntity<OR<Void>>(HttpStatus.OK);
    }

    public <T> ResponseEntity<T> process(Supplier<T> sp) {
        return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(sp.get());
    }

    public void processException(OR or, Exception ex) {
        if (ex instanceof DtsException) {
            DtsException dtsException = (DtsException) ex;
            or.setCode(dtsException.getCode());
            or.setResult(false);
            or.setMsg(ex.getMessage());
            or.setHttpStatus(dtsException.getStatusCode());
        } else {
            // 系统错误设置RequestId，方便开发人员排查错误日志
            or.setCode("500");
            or.setResult(false);
            String requestId = UUID.randomUUID().toString();
            log.error("[BaseController] Exception details of requestId: " + requestId, ex);
            or.setMsg(String.format("请求编号: %s, 错误消息: %s", requestId, ex.getMessage()));
            or.setHttpStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        }
    }

    public String currentUser() {
        HttpServletRequest request = request();
        String token = this.request().getHeader("token");
        if (StringUtils.isBlank(token)) {
            token = request.getParameter("token");
        }
        if(StringUtils.isBlank(token)){
            throw new DtsException(201,"201","token不存在");
        }
        TokenInfoVO tokenInfoVo = JwtUtils.decodeJwt(token);
        return tokenInfoVo.getUserId();
    }

    public HttpServletRequest request() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        assert requestAttributes != null;

        return requestAttributes.getRequest();
    }
}
